from sqlalchemy import Column, Integer, String, Text
from sqlalchemy.orm import declarative_base, sessionmaker
from database.mysql_utils import MysqlUtils

Base = declarative_base()


class User(Base):
    __tablename__ = 'users'

    # 定义表的列
    id = Column(Integer, primary_key=True)
    uid = Column(Integer)
    realname = Column(String(255))
    username = Column(String(255))
    mobile = Column(String(255))
    email = Column(String(255))
    login_num = Column(Integer)
    last_login_time = Column(String(255))
    last_login_ip = Column(String(255))
    status = Column(Integer)
    create_time = Column(String(255))
    role = Column(String(255))
    seller = Column(Text)
    is_master = Column(String(255))


Base.metadata.create_all(MysqlUtils.mysql_connect())
Session = sessionmaker(bind=MysqlUtils.mysql_connect())



class UserData:

    @staticmethod
    def insert_user_info(all_user_data):
        session = Session()
        for one_store_info in all_user_data:
            uid = one_store_info.get('uid')
            realname = one_store_info.get('realname')
            username = one_store_info.get('username')
            mobile = one_store_info.get('mobile')
            email = one_store_info.get('email')
            login_num = one_store_info.get('login_num')
            last_login_time = one_store_info.get('last_login_time')
            last_login_ip = one_store_info.get('last_login_ip')
            status = one_store_info.get('status')
            create_time = one_store_info.get('create_time')
            role = one_store_info.get('role')
            seller = one_store_info.get('seller')
            is_master = one_store_info.get('is_master')
            user = User(
                uid=uid,
                realname=realname,
                username=username,
                mobile=mobile,
                email=email,
                login_num=login_num,
                last_login_time=last_login_time,
                last_login_ip=last_login_ip,
                status=status,
                create_time=create_time,
                role=role,
                seller=seller,
                is_master=is_master
            )
            session.add(user)
        session.commit()
        session.close()

    @staticmethod
    def delete_user_info():
        session = Session()
        session.query(User).delete()
        session.commit()
        session.close()

    @staticmethod
    def get_user_id_list():
        session = Session()
        ret = session.query(User.uid).all()
        user_id_list = []
        if ret:
            user_id_list = [uid_obj[0] for uid_obj in ret]
        session.close()
        return user_id_list

    @staticmethod
    def get_mobile_by_uid(uid):
        session = Session()
        ret = session.query(User.mobile).filter(User.uid==uid).first()
        print(ret[0])
        session.close()
        return ret[0]

    @staticmethod
    def get_mobile_by_username(username):
        session = Session()
        ret = session.query(User.mobile).filter(User.realname==username).first()
        return ret[0]

    @staticmethod
    def get_username_by_uid(uid):
        session = Session()
        ret = session.query(User.realname).filter(User.uid == uid).first()
        print(ret[0])
        return ret[0]

if __name__ == '__main__':
    # UserData.get_mobile_by_uid("10587971")
    UserData.get_username_by_uid("10587971")